#include "npc6.h"

#include "path.h"
#include "../effect.h"
#include "../gun/gun.h"
#include "../common.h"

namespace fireplane{
	NPC6::NPC6(EnemyType type):
		Enemy(type),
		_fire_tick(0),
		_fire_middle_gun_tick(0),
		_is_deading(false),
		_middleGun(NULL),
		_is_need_fire_middle(false),bulet_cnt(0),
		_fire_tick_left_right(0),_is_need_fire_left_right(false),bulet_cnt_left_right(0),
		_fire_step(0),
		_is_fire_ji_guang(false)
		{}

		bool NPC6::updateState( float dt ){
			bool ret = true;

			if (isDestroy()){
				return false;
			}

			if (_is_attak){
				_attack_back_time += dt;
				if (_attack_back_time > 0.5f){
					_attack_back_time = 0;
					_is_attak = false;
					_sprite->setColor(ccc3(255,255,255));
			}
		}

		CCPoint pos = this->getPosition();
		if (pos.y < -50){
			ret =  false;
		}

		list<CCRect>::iterator it = _all_collision_rects.begin();
		for (;it != _all_collision_rects.end();it ++){
			CCRect& rect = (*it);
			rect.origin.x = pos.x - rect.size.width / 2;
			rect.origin.y = pos.y - rect.size.height / 2;
		}

		if(!_is_deading){
			{
				switch(_fire_step){
				case 0:
					{
						if(_is_fire_ji_guang){
							_fire_tick += dt;
							if(_fire_tick > 1.0f){
								_fire_tick = 0;
								_is_fire_ji_guang = false;
								_middleGun->setBulletType(Bullet::BT_ji_guang);
								_middleGun->fire(Bullet::BI_img0);
								_middleGun->setBulletType(Bullet::BT_dir_bullet);
							}
						}
						else{
							if(!_is_need_fire_middle){
								_fire_tick += dt;
								if(_fire_tick > 2.0f){
									_fire_tick = 0;
									_is_need_fire_middle = true;
								}
							}
							else{
								_fire_tick += dt;
								if(_fire_tick > 0.1f){
									_fire_tick = 0;
									_middleGun->fire(Bullet::BI_img6);

									bulet_cnt ++;
									if(bulet_cnt > 3){
										bulet_cnt = 0;
										_is_need_fire_middle = false;

										_fire_step = 1;
									}
								}
							}
						}
					}
					break;

				case 1:
					{
						if(!_is_need_fire_left_right){
							_fire_tick_left_right += dt;
							if(_fire_tick_left_right > 1.0f){
								_fire_tick_left_right = 0;
								_is_need_fire_left_right = true;
							}
						}
						else{
							_fire_tick_left_right += dt;
							if(_fire_tick_left_right > 0.1f){
								_fire_tick_left_right = 0;
								_leftGun[0]->fire(Bullet::BI_img7);
								_leftGun[1]->fire(Bullet::BI_img7);
								_rightGun[0]->fire(Bullet::BI_img7);
								_rightGun[1]->fire(Bullet::BI_img7);

								bulet_cnt_left_right ++;
								if(bulet_cnt_left_right > 3){
									bulet_cnt_left_right = 0;
									_is_need_fire_left_right = false;

									_fire_step = 0;
									_is_fire_ji_guang = true;
								}
							}
						}
					}
					break;
				}




				

				
				// create effect
				CCPoint worldpos = this->getPosition();
				worldpos = this->getParent()->convertToWorldSpace(worldpos);

				Effect::CreateNpc4Bomb0 cmd;
				cmd._id = Effect::ET_npc41;
				cmd.worldPos = worldpos;

				Effect::createEffect(&cmd);
			}
		}

		return ret;
	}

	NPC6::~NPC6(){}

	void NPC6::onInit(CreateArg* arg){
		CreateNormalEnemy* targ = (CreateNormalEnemy*)arg;

		_sprite = CCSprite::create("enemy/npc6/body.png");
		addChild(_sprite);
		this->setPosition(targ->pos);

		CCMoveBy* m0 = CCMoveBy::create(0.1f,ccp(0,-5));
		CCMoveBy* m1 = CCMoveBy::create(0.1f,ccp(0,5));
		CCSequence* seq = CCSequence::create(m0,m1,NULL);
		CCRepeatForever* action = CCRepeatForever::create(seq);
		_sprite->runAction(action);

		CCSize contentsize = _sprite->getContentSize();
		CCRect rect = CCRectMake(targ->pos.x - contentsize.width / 2,targ->pos.y - contentsize.height / 2,contentsize.width,
			contentsize.height);

		_all_collision_rects.push_back(rect);

		_hp = 2000;
		_is_attak = false;
		_attack_back_time = 0;
		_cur_step = 1;
		_speed = 200.0f;

		Gun::CreateNormalGun arg0(Gun::GT_normal_gun);
		arg0.tdir = ccp(0,-1);
		arg0.tspeed = 1000;
		arg0.o = Bullet::BO_ENEMY;
		arg0.pos = ccp(0,-185);
		

		_middleGun = Gun::createGun(&arg0);
		addChild(_middleGun);

#define _START_VECTOR_Y_ (3.0f)
#define _DETLA_ANGLE_ (12.0f)
		float angle = 0;
		for(int i = 0;i < 2;i++)
		{
			Gun::CreateNormalGun arg0(Gun::GT_normal_gun);
			arg0.tdir = ccp(-1,-_START_VECTOR_Y_);
			arg0.tdir = ccpNormalize(arg0.tdir);

			CCAffineTransform t = CCAffineTransformMakeIdentity();
			t = CCAffineTransformRotate(t,angle);
			arg0.tdir = Common::CCPointApplyAffineTransform(arg0.tdir,t);

			arg0.tspeed = 800;
			arg0.o = Bullet::BO_ENEMY;
			arg0.pos = ccp(-100,-135);

			_leftGun[i] = Gun::createGun(&arg0);
			addChild(_leftGun[i]);

			angle += CC_DEGREES_TO_RADIANS(_DETLA_ANGLE_);
		}

		angle = 0;
		for(int i = 0;i < 2;i++)
		{
			Gun::CreateNormalGun arg0(Gun::GT_normal_gun);
			arg0.tdir = ccp(1,-_START_VECTOR_Y_);
			arg0.tdir = ccpNormalize(arg0.tdir);

			CCAffineTransform t = CCAffineTransformMakeIdentity();
			t = CCAffineTransformRotate(t,angle);
			arg0.tdir = Common::CCPointApplyAffineTransform(arg0.tdir,t);

			arg0.tspeed = 800;
			arg0.o = Bullet::BO_ENEMY;
			arg0.pos = ccp(100,-135);
			

			_rightGun[i] = Gun::createGun(&arg0);
			addChild(_rightGun[i]);

			angle += CC_DEGREES_TO_RADIANS(-_DETLA_ANGLE_);
		}
	}

	void NPC6::destroy()
	{
		Enemy::destroy();
	}

	void NPC6::onCollision2Player(){
		if(_is_deading){
			return;
		}

		_onAttack(-_hp);
	}

	void NPC6::_onAttack(int addHp){
		if(_is_deading){
			return;
		}

		_hp += addHp;
		if (_hp <= 0){
			_hp = 0;
			_is_deading = true;
			_sprite->stopAllActions();
			
			// create effect
			CCPoint worldpos = this->getPosition();
			worldpos = this->getParent()->convertToWorldSpace(worldpos);

			Effect::CreateNpc4Bomb0 cmd;
			cmd._id = Effect::ET_npc4;
			cmd.worldPos = worldpos;

			Effect::createEffect(&cmd);
		}

		if(!_is_deading){
			_sprite->setColor(ccc3(180,0,0));
			_is_attak = true;
			_attack_back_time = 0;
		}
		else{
			CCPoint pos = _sprite->getPosition();
			_sprite->initWithFile("enemy/npc5/destroy.png");
			_sprite->setPosition(pos);
			pos.x += 10;
			pos.y -= 10;
			CCMoveTo* m0 = CCMoveTo::create(0.4f,pos);
			_sprite->runAction(m0);
			_sprite->runAction(CCScaleTo::create(0.4f,0.5f));
		}
	}

	void NPC6::onAttack()
	{
		if(_is_deading){
			return;
		}

		_onAttack(-10);
	}
}